﻿using System.Web;

namespace Framework.TypeExtension
{
    public class SessionExtension
    {
        /// <summary>
        /// 创建一个Session对象
        /// </summary>
        /// <param name="key">键名</param>
        /// <param name="value">键值</param>
        /// <param name="time">保存时间(分钟为单位)</param>
        /// <returns></returns>
        public static bool Add(string key, string value, int time)
        {
            try
            {
                HttpContext.Current.Session[key] = value;
                HttpContext.Current.Session.Timeout = time;
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 获取一个Session对象
        /// </summary>
        /// <param name="key">键名</param>
        /// <returns></returns>
        public static object Value(string key)
        {
            return HttpContext.Current.Session[key];
        }
        /// <summary>
        /// 判断一个Session对象是否存在
        /// </summary>
        /// <param name="key">键名</param>
        /// <returns></returns>
        public static bool Exis(string key)
        {
            try
            {
                var o = HttpContext.Current.Session[key];
                if (o != null && true & o.ToString() != "")
                {
                    return true;
                }
                return false;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一个Session对象
        /// </summary>
        /// <param name="key">键名</param>
        public static void Remove(string key)
        {
            HttpContext.Current.Session.Remove(key);
        }
    }
}
